<template>
  <Panel title="修改密码">
    <!-- 主要内容 -->
    <div slot="container">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="small"
      >
        <!-- 原密码 -->
        <el-form-item label="原密码" prop="oldPwd">
          <el-input
            type="password"
            v-model="ruleForm.oldPwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 新密码 -->
        <el-form-item label="新密码" prop="newPwd">
          <el-input
            type="password"
            v-model="ruleForm.newPwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="surePass">
          <el-input
            type="password"
            v-model="ruleForm.surePass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="small" @click="savePass"
            >确定</el-button
          >
          <el-button size="small" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </Panel>
</template>

<script>
import Panel from "@/components/Panel.vue";

//引入验证规则
import { checkPass } from "@/utils/validate.js";

//引入ajax接口

import { reqCheckOld, reqEditPass } from "@/api/user";

export default {
  components: {
    Panel,
  },

  data() {
    //验证原密码
    var validateOldPwd = async (rule, value, callback) => {
      if (value == "") {
        callback(new Error("原密码不能为空"));
      } else if (!checkPass(value)) {
        callback(
          new Error(
            "密码不符合规则：以字母开头,长度在6~18之间,包含字母、数字和下划线"
          )
        );
      } else {
        //检查旧密码是否正确的ajax发送
        let res = await reqCheckOld({ oldPwd: value });
        console.log(res);
        if (res.data.code == 200) {
          // this.$message.success(res.data.msg);
          callback();
        }
      }
    };

    //验证新密码
    var validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("密码不能为空"));
      } else if (!checkPass(value)) {
        callback(
          new Error(
            "密码不符合规则：以字母开头,长度在6~18之间,包含字母、数字和下划线"
          )
        );
      } else if (value == this.ruleForm.oldPwd) {
        callback(new Error("新密码不能与原密码一样"));
      } else {
        //密码和确认密码一致性校验
        //表示确认密码不为空时,再次重新验证确认密码
        if (this.ruleForm.surePass !== "") {
          this.$refs.ruleForm.validateField("surePass");
        }
        callback();
      }
    };
    //确认密码
    var validateSurePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("确认密码不能为空"));
      } else if (value != this.ruleForm.newPwd) {
        callback(new Error("两次输入密码不一致，请重新输入"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        oldPwd: "",
        newPwd: "",
        surePass: "",
      },
      rules: {
        //自定义校验规则
        oldPwd: { required: true, validator: validateOldPwd, trigger: "blur" },
        newPwd: { required: true, validator: validatePass, trigger: "blur" },
        surePass: {
          required: true,
          validator: validateSurePass,
          trigger: "blur",
        },
      },
    };
  },

  methods: {
    savePass() {
      this.$refs["ruleForm"].validate(async (valid) => {
        //valid 表示表单所有字段是否通过验证,只要有一个没有通过,返回false,否则返回true
        if (valid) {
          ///可以发ajax进行登陆操作
          let res = await reqEditPass({ newPwd: this.ruleForm.newPwd });
          //判定
          if (res.data.code == 0) {
            //提示
            // this.$message.success(res.data.msg)
            //清空本地存储数据
            localStorage.clear();
            //跳转到登陆页
            this.$router.push("/login");
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input {
  width: 200px;
}
</style>